// 封装action
import { good_fav, good_unfav, good_favlist, shopcart_list, shopcart_add, shopcart_sub } from '../utils/api'

// 创建收藏同步动作
function create_collect_action(params) {
    return { type: 'COLLECT', params }
}
// 创建取消收藏同步动作
function create_cancel_collect_action(params) {
    return { type: 'CANCELCOLLECT', params }
}
// 创建收藏列表同步动作
function create_collectlist_action(params) {
    return { type: 'COLLECTLIST', params }
}
// 创建购物车列表同步动作
function create_shopcartlist_action(params) {
    return { type: 'SHOPCARTLIST', params }
}
// 创建购物车+1同步动作
function create_add_shopcart_action(params) {
    return { type: 'ADDSHOPCART', params }
}
// 创建购物车-1同步动作
function create_sub_shopcart_action(params) {
    return { type: 'SUBSHOPCART', params }
}

// 创建收藏异步动作
function request_collect_action(params) {
    return function(dispatch) {
        var userinfo = localStorage.getItem('userinfo') ? JSON.parse(localStorage.getItem('userinfo')) : {}
        good_fav({ userid: userinfo.id, goodid: params.id }).then((res) => {
            if (res.data.code === 200) {
                dispatch(create_collect_action(params))
            }
        })
    }
}
// 创建取消收藏异步动作
function request_cancel_collect_action(params) {
    return function(dispatch) {
        var userinfo = localStorage.getItem('userinfo') ? JSON.parse(localStorage.getItem('userinfo')) : {}
        good_unfav({ userid: userinfo.id, goodid: params.id }).then((res) => {
            if (res.data.code === 200) {
                dispatch(create_cancel_collect_action(params))
            }
        })
    }
}
// 创建收藏列表异步动作
function request_collectlist_action(params) {
    return function(dispatch) {
        var userinfo = localStorage.getItem('userinfo') ? JSON.parse(localStorage.getItem('userinfo')) : {}
        good_favlist({ userid: userinfo.id }).then((res) => {
            if (res.data.code === 200) {
                dispatch(create_collectlist_action(res.data.list))
            }
        })
    }
}
// 创建购物车列表异步动作
function request_shopcartlist_action(params) {
    return function(dispatch) {
        var userinfo = localStorage.getItem('userinfo') ? JSON.parse(localStorage.getItem('userinfo')) : {}
        shopcart_list({ userid: userinfo.id }).then((res) => {
            if (res.data.code === 200) {
                console.log(res);
                dispatch(create_shopcartlist_action(res.data.list))
            }
        })
    }
}
// 创建购物车+1异步动作
function request_add_shopcart_action(params) {
    return function(dispatch) {
        var userinfo = localStorage.getItem('userinfo') ? JSON.parse(localStorage.getItem('userinfo')) : {}
        shopcart_add({ userid: userinfo.id, goodid: params.id }).then((res) => {
            if (res.data.code === 200) {
                dispatch(create_add_shopcart_action(params))
                dispatch(request_shopcartlist_action(params))
            }
        })
    }
}
// 创建购物车-1异步动作
function request_sub_shopcart_action(params) {
    return function(dispatch) {
        var userinfo = localStorage.getItem('userinfo') ? JSON.parse(localStorage.getItem('userinfo')) : {}
        shopcart_sub({ userid: userinfo.id, goodid: params.id }).then((res) => {
            if (res.data.code === 200) {
                dispatch(create_sub_shopcart_action(params))
                dispatch(request_shopcartlist_action(params))
            }
        })
    }
}

export {
    request_collect_action,
    request_cancel_collect_action,
    request_collectlist_action,
    request_shopcartlist_action,
    request_add_shopcart_action,
    request_sub_shopcart_action
}